#include "drawpicture.h"
#include "ui_drawpicture.h"


drawPicture::drawPicture(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::drawPicture)
{
    ui->setupUi(this);
    setWindowTitle("绘图页面");
    setFixedSize(1000,660);//设置固定大小

    pix = new QPixmap(1000,660); //画布大小
    pix->fill(Qt::white); //画布填充白色
    pix2 = new QPixmap(1000,660);
    pix2->fill(Qt::white);
    pen = new QPen(Qt::black);

    pen->setWidth(2); //粗细
    pen->setStyle(Qt::DashDotDotLine);//设置画笔风格

    addCBox();

}

drawPicture::~drawPicture()
{
    delete ui;
}

void drawPicture::on_btn_save_clicked()
{
    pix->save("./img.png");
    QMessageBox::information(this,"提示框","保存成功");
}
void drawPicture::on_btn_del_clicked()
{

    if(QMessageBox::Yes==QMessageBox::question(this,"提醒框","确定删除？",QMessageBox::No,QMessageBox::Yes))
    {
        pix->fill(Qt::white);
        update();
    }
}

void drawPicture::paintEvent(QPaintEvent* ev)
{
    ev->ignore();
    QPainter painter;
    painter.begin(this);
    painter.drawPixmap(0,50,*pix);//工具栏不能使用
    painter.end();
}

void drawPicture::on_btn_pen_clicked()
{
    penShape=DefaultShape;
    pen->setColor(QColor(Qt::black));
    pen->setWidth(2); //粗细
    pen->setStyle(Qt::SolidLine);//设置画笔风格

}

void drawPicture::on_ben_erasure_clicked()//擦除
{
    penShape=DefaultShape;
    pen->setColor(QColor(Qt::white));
    pen->setWidth(25); //粗细
    pen->setStyle(Qt::SolidLine);//设置画笔风格
}


void drawPicture::on_btn_line_clicked()//直线
{
    penShape=Line;
}

void drawPicture::on_btn_rect_clicked()//矩形
{
    penShape=Rect;
}

void drawPicture::on_btn_round_clicked()//圆
{
    penShape=Round;
}


void drawPicture::on_btn_color_clicked()//设置笔刷颜色
{
    pen->setColor(QColorDialog::getColor());
}

void drawPicture::on_cBox_width_currentIndexChanged(const QString &arg1)//线宽
{
    pen->setWidth(arg1.toInt());
}

void drawPicture::on_cBox_style_currentIndexChanged(const QString &arg1)//线类型
{
    if(arg1 == "实线")
        pen->setStyle(Qt::PenStyle::SolidLine);
    if(arg1 == "虚线")
        pen->setStyle(Qt::PenStyle::DashLine);
    if(arg1=="点线")
        pen->setStyle(Qt::PenStyle::DashDotLine);
    else
        return;
}

void drawPicture::mousePressEvent(QMouseEvent* ev)
{
    *pix2=pix->copy();
    point=ev->pos();//获取鼠标点击坐标

    if(flag==true)
    {
        QTextEdit* textEdit=new QTextEdit("文本框",this);
        textEdit->setGeometry(point.x(),point.y(),100,90);
        textEdit->setStyleSheet("QTextEdit { background-color: transparent; border: 2px solid black; }");
        flag=false;
        textEdit->show();
    }

}

void drawPicture::mouseMoveEvent(QMouseEvent* ev)
{
    QString str=QString("鼠标 x=%1 y=%2 ").arg(ev->x()).arg(ev->y());
    ui->lbl_point->setText(str);
    //起点位于工具栏
    if(ev->pos().y()<50)
    {
        return;
    }
    //避免点变换的时候，每次都绘图
    if(penShape==Line || penShape==Rect || penShape==Round)
    {
        *pix=pix2->copy();
    }

    QPainter painter;
    painter.begin(pix);
    painter.setPen(*pen);

    switch (penShape)
    {
    case DefaultShape:
        painter.drawLine(point.x(),point.y()-50,ev->x(),ev->y()-50);
        point=ev->pos();//画曲线，更新节点
        break;
    case Line:
        painter.drawLine(point.x(),point.y()-50,ev->x(),ev->y()-50);
        break;
    case Rect:
        painter.drawRect(point.x(),point.y()-50,ev->x()-point.x(),ev->y()-point.y());
        break;
    case Round:
        painter.drawEllipse(point.x(),point.y()-50,ev->x()-point.x(),ev->y()-point.y());
        break;
    default:
        break;
    }

    painter.end();
    update();

}


void drawPicture::addCBox()
{
    ui->cBox_width->addItem("1");
    ui->cBox_width->addItem("2");
    ui->cBox_width->addItem("8");
    ui->cBox_style->addItem("实线");
    ui->cBox_style->addItem("虚线");
    ui->cBox_style->addItem("点线");
}



void drawPicture::on_btn_text_clicked()
{
    flag=true;
}
